package com.deere.workprocedure.service;

import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.deere.common.ExcelHandler;
import com.deere.model.ItemWorkflow;
import com.deere.model.dto.ItemWorkflowDto;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class ItemWorkflowServiceTestCase {
	@Autowired
	private ItemWorkflowService iwService;
	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSerachWorkingProcedure() {
		fail("Not yet implemented");
	}

	@Test
	public void testCalcWorkingProcedurePrice() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetItemWorkflow() {
		ItemWorkflow f  = new ItemWorkflow();
		f.setPartCode("0-1Mpa");
		ItemWorkflow itemWorkflows = iwService.getItemWorkflow("0-1Mpa");
		assertNull(itemWorkflows);
	}
	@Test
	public void testInsertItemWorkflow() {
		ItemWorkflow itemWorkflow = new ItemWorkflow(); 

		itemWorkflow.setPartCode("BLHS24-09");
		itemWorkflow.setWpCode("XIAN");
		itemWorkflow.setSequence(1);
		itemWorkflow.setManHours(5);
		//itemWorkflow.setProcedures(procedures);
		iwService.insertItemWorkflow(itemWorkflow);
		//
		itemWorkflow = new ItemWorkflow(); 

		itemWorkflow.setPartCode("BLHS24-09");
		itemWorkflow.setWpCode("QIAN");
		itemWorkflow.setSequence(2);
		itemWorkflow.setManHours(5.65f);
		
		//itemWorkflow.setProcedures(procedures);
		iwService.insertItemWorkflow(itemWorkflow);
		itemWorkflow = new ItemWorkflow(); 
		
		itemWorkflow.setPartCode("BLHS24-09");
		itemWorkflow.setWpCode("BAN");
		itemWorkflow.setSequence(3);
		itemWorkflow.setManHours(9);
		//itemWorkflow.setProcedures(procedures);
		iwService.insertItemWorkflow(itemWorkflow);
	}
	@Test
	public void testRemoveItemWorkflow() {
		ItemWorkflow itemWorkflow = new ItemWorkflow(); 
		iwService.removeItemWorkflow(itemWorkflow);
	}
	@Test
	public void testGetMaxWPCodeSequence() {
		ItemWorkflow itemWorkflow = new ItemWorkflow(); 
		itemWorkflow.setPartCode("0-1Mpa");
		itemWorkflow.setWpCode("BAN2");
		int max = iwService.getMaxWPCodeSequence(itemWorkflow);
		System.out.println(max);
	}
	@Test
	public void testGenerateWorkingProcedure() {
		ItemWorkflow itemWorkflow = new ItemWorkflow(); 
		itemWorkflow.setPartCode("2-[6]923(B)");
		itemWorkflow.setWpCode("BAN");
		iwService.generateWorkingProcedure(itemWorkflow);
	}
	/**
	 * 从文件初始化零件的整个加工流程
	 * @throws URISyntaxException 
	 */
	@Test
	public void testInsertItemWorkflowFromFile() throws URISyntaxException {
		System.out.println(this.getClass().getResource("/").getPath());
		URL url = this.getClass().getClassLoader().getResource("工时.xls");
		System.out.println(url.toURI().getPath());
		File file = new File(this.getClass().getClassLoader().getResource("工时.xls").toURI().getPath());
		String pattern = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
		ExcelHandler<ItemWorkflowDto> handler = new ExcelHandler<ItemWorkflowDto>(ItemWorkflowDto.class);
		List<ItemWorkflowDto> workflowDtos ;//= (List<ItemWorkflowDto>) handler.importExcel(file, pattern,0);
		//System.out.println(workflowDtos.size());
		int offset = 0;
		int length = 100;
		while(null != (workflowDtos = (List<ItemWorkflowDto>) handler.importExcel(file, pattern,offset,length)) && workflowDtos.size() > 0){
			offset+=length;
			List<ItemWorkflow> fullWorkflow = new ArrayList<ItemWorkflow>();
			Map<String,ItemWorkflow> iwCache = new HashMap<String, ItemWorkflow>();
			for(int i = 0;i < workflowDtos.size(); i++){
				//fullWorkflow.clear();
				
				ItemWorkflowDto dto = workflowDtos.get(i);
				if(dto.getChe() > 0){
					ItemWorkflow iw;
					if(iwCache.containsKey("CHE")){
						iw = iwCache.get("CHE");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("CHE");
						iwCache.put("CHE", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getChe());
					fullWorkflow.add(iw);
				}
				if(dto.getXian() > 0){
					ItemWorkflow iw;
					if(iwCache.containsKey("XIAN")){
						iw = iwCache.get("XIAN");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("XIAN");
						iwCache.put("XIAN", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getXian());
					fullWorkflow.add(iw);
				}
				if(dto.getPao() > 0){
					ItemWorkflow iw;
					if(iwCache.containsKey("PAO")){
						iw = iwCache.get("PAO");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("PAO");
						iwCache.put("ME", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getPao());
					fullWorkflow.add(iw);
				}
				if(dto.getMe() > 0){
					ItemWorkflow iw;
					if(iwCache.containsKey("ME")){
						iw = iwCache.get("ME");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("ME");
						iwCache.put("ME", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getMe());
					fullWorkflow.add(iw);
				}
				if(dto.getQian() > 0){
					ItemWorkflow iw ;
					if(iwCache.containsKey("QIAN")){
						iw = iwCache.get("QIAN");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("QIAN");
						iwCache.put("QIAN", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getQian());
					fullWorkflow.add(iw);
				}
				if(dto.getBan() > 0){
					ItemWorkflow iw ;
					if(iwCache.containsKey("BAN")){
						iw = iwCache.get("BAN");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("BAN");
						iwCache.put("BAN", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getBan());
					fullWorkflow.add(iw);
				}
				if(dto.getTang() > 0){
					ItemWorkflow iw ;
					if(iwCache.containsKey("TANG")){
						iw = iwCache.get("TANG");
					}else{
						iw = new ItemWorkflow();
						iw.setWpCode("TANG");
						iwCache.put("TANG", iw);
					}
					iw.setPartCode(dto.getPartCode());
					iw.setManHours(dto.getTang());
					fullWorkflow.add(iw);
				}
				
				iwService.insertItemWorkflows(fullWorkflow);
			}
		}
	}

}
